<template>
  <view id="segmented" class="segmented">
    <view class="segmented-control">
        <view :id="'sc-'+index" :data-index="index" v-for="(item, index) in values" class="segmented-control-item" :key="index" :class="index === current ? 'active' : ''"
            @click="onClick">
            {{item}}
        </view>
    </view>
  </view>
    
</template>

<script>
export default {
  name: 'segmented-control',
  props: {
    values: { // 要显示的数组
      type: Array,
      default() {
        return [];
      }
    },
    current: { // 当前选中第几个
      type: Number,
      default(){
        return 1;
      }
    }
  },
  methods: {
    /**
     * 点击事件
     */
    onClick(e) {
      let index = parseInt(e.mp.target.dataset.index);
      let that = this;
      let id = e.mp.target.id;
      if (that.current !== index) {
        that.$emit('clickItem', index); // 父级组件回调方法
      }
    }
  },
  mounted() {
  }
}
</script>

<style lang="less">
.segmented {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 100;
}
.segmented-control {
  display: flex;
  background: #ffffff; 
  align-items: center;
  justify-content: space-around;
  border-bottom: 1px solid #f6f6f6;
}
 .segmented-control-item {
    font-size: 24upx;
	text-align: center;
	padding: 20upx 0;
    color: #999;
  }
  .active {
	 color: #DBC291;
	  border-bottom: 6upx solid #DBC291;
	  // background: url("https://xpandago.oss-cn-shanghai.aliyuncs.com/applet/tab_cp.png") no-repeat bottom center;
	   background-size: 200upx;
  }
</style>